<template>
  <div class="app-container">
    <!--<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导入时间" prop="addDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.addDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择导入时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否有过期数据" prop="hasExpired">
        <el-input
          v-model="queryParams.hasExpired"
          placeholder="请输入是否有过期数据"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleImport"
          v-hasPermi="['equipment:tzsb:import']"
          >覆盖导入</el-button
        >
      </el-col>

      <!--<el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['equipment:tzsb:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tzsbList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="所属基地" align="center" prop="deptName" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column
        label="导入时间"
        align="center"
        prop="addDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="是否有过期数据"
        align="center"
        prop="hasExpired"
        :formatter="hasExpiredFormat"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            v-if="scope.row.hasExpired == 1"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleExpireView(scope.row)"
            >过期数据</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:tzsb:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:tzsb:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 设备导入对话框 -->
    <el-dialog
      v-myFullscreen
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <!--<el-form-item label="计划月份" prop="planDate">-->
      <!-- <el-date-picker clearable size="small" style="margin-bottom: 20px" v-model="pDate" type="month" value-format="yyyy-MM-dd" placeholder="选择计划月份">
      </el-date-picker>-->

      <!--</el-form-item>-->
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />
          <el-link type="info" style="font-size: 12px" @click="importTemplate"
            >下载模板</el-link
          >
        </div>
        <div class="el-upload__tip" style="color: red" slot="tip">
          提示：仅允许导入“xls”或“xlsx”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改特种设备管理对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
      v-myDialogDrag
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入名称"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="导入时间" prop="addDate">
              <el-date-picker
                clearable
                size="small"
                v-model="form.addDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择导入时间"
                :disabled="true"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <!--<el-form-item label="是否有过期数据" prop="hasExpired">
                  &lt;!&ndash;<el-input v-model="form.hasExpired" placeholder="请输入是否有过期数据" :disabled="true" />&ndash;&gt;
                  <el-option v-for="dict in hasExpiredOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" :disabled="true" />
                </el-form-item>-->
            <el-form-item label="是否有过期数据" prop="hasExpired">
              <el-select
                v-model="form.hasExpired"
                placeholder=""
                clearable
                size="small"
                :disabled="true"
              >
                <el-option
                  v-for="dict in hasExpiredOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTzsb,
  getTzsb,
  delTzsb,
  addTzsb,
  updateTzsb,
} from "@/api/equipment/tzsb/tzsb";
import { getToken } from "@/utils/auth";
export default {
  name: "Tzsb",
  data() {
    return {
      importTemplateUrl: "",
      importTemplateName: "",
      infoUrl: "",
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 特种设备管理表格数据
      tzsbList: [],
      hasExpiredOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        name: null,
        addDate: null,
        hasExpired: null,
        type: null,
        deptName: null,
      },
      // 导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/equipment/tzsb/import",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    if (this.$route.query.type == "1") {
      this.infoUrl = "/equipment/tzsb/info";
      this.importTemplateUrl = "equipment/tzsb/importTemplate";
      this.importTemplateName = `特种设备导入模板.xlsx`;
      this.upload.url = process.env.VUE_APP_BASE_API + "/equipment/tzsb/import";
      this.upload.title = "特种设备导入";
    }
    if (this.$route.query.type == "2") {
      this.infoUrl = "/equipment/tzsb/zyzsInfo";
      this.importTemplateUrl = "equipment/zyzsInfo/importTemplate";
      this.importTemplateName = `作业证书导入模板.xlsx`;
      this.upload.url =
        process.env.VUE_APP_BASE_API + "/equipment/zyzsInfo/import";
      this.upload.title = "作业证书导入";
    }
    if (this.$route.query.type == "3") {
      this.infoUrl = "/equipment/tzsb/aqfylbInfo";
      this.importTemplateUrl = "equipment/aqfylbInfo/importTemplate";
      this.importTemplateName = `安全阀压力表导入模板.xlsx`;
      this.upload.url =
        process.env.VUE_APP_BASE_API + "/equipment/aqfylbInfo/import";
      this.upload.title = "安全阀压力表导入";
    }
    if (this.$route.query.type == "4") {
      this.infoUrl = "/equipment/tzsb/otherInfo";
      this.upload.title = "其他证书导入";
    }
    this.queryParams.type = this.$route.query.type;
    this.getList();
    this.getDicts("has_expired").then((response) => {
      this.hasExpiredOptions = response.data;
    });
  },
  methods: {
    /** 查询特种设备管理列表 */
    getList() {
      this.loading = true;
      listTzsb(this.queryParams).then((response) => {
        this.tzsbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 字典翻译
    hasExpiredFormat(row, column) {
      return this.selectDictLabel(this.hasExpiredOptions, row.hasExpired);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        addDate: null,
        hasExpired: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加特种设备管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getTzsb(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改特种设备管理";
      });
    },
    /** 查看按钮 */
    /*handleView(row) {
        this.reset();
        this.openDisabled = true;
        const id = row.id || this.ids
        getTzsb(id).then(response => {
           this.form = response.data;
           this.open = true;
           this.title = "查看特种设备管理";
        });
    },*/

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },

    submitFileForm() {
      this.$refs.upload.submit();
    },

    /** 查看按钮 */
    handleView(row) {
      this.$router.push({
        path: this.infoUrl,
        query: {
          parentId: row.id,
        },
      });
    },

    /** 查看按钮 */
    handleExpireView(row) {
      this.$router.push({
        path: this.infoUrl,
        query: {
          parentId: row.id,
          expiredFlag: "1",
        },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTzsb(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const rLoading = this.openLoading();
            addTzsb(this.form)
              .then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                rLoading.close();
                this.getList();
              })
              .catch((error) => {
                rLoading.close();
              });
          }
        }
      });
    },

    /** 导入按钮操作 */
    handleImport() {
      /*this.upload.title = "特种设备导入";*/
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        this.importTemplateUrl,
        {
          ...this.queryParams,
        },
        this.importTemplateName
      );
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delTzsb(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/tzsb/export",
        {
          ...this.queryParams,
        },
        "特种设备管理" + this.parseTime(new Date()) + ".xlsx"
      );
    },
  },
};
</script>
